<?php

namespace App\Http\Controllers\Admin\Editor;
use App\Http\Requests\ArticlesPostRequest;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    public function getIndex(Request $request)
    {
        // $articles = DB::table('hs_articles')->join('hs_books','hs_books.id','=','hs_articles.bid')->where(['hs_articles.aid'=>1,'hs_articles.status'=>0])->select('hs_articles.*','hs_books.b_name')->paginate(4);
        $articles = DB::table('hs_articles')
                    ->join('hs_member','hs_member.id','=','hs_articles.aid')
                    ->join('hs_relation','hs_relation.aid','=','hs_member.id')
                    ->join('hs_editor','hs_editor.id','=','hs_relation.eid')
                    ->join('hs_books','hs_books.id','=','hs_articles.bid')
                    ->where(['hs_editor.id'=>$request->session()->get('id'),'hs_articles.status'=>'0'])
                    ->select('hs_articles.id','hs_articles.title','hs_articles.isfree','hs_articles.status','hs_member.name','hs_books.b_name')
                    ->paginate(4);
                    // ->get();

        // dd($articles);
        return view('admin.editor.article.index',['articles'=>$articles]);
    }

    public function getYindex(Request $request)
    {
        $articles = DB::table('hs_articles')
                    ->join('hs_member','hs_member.id','=','hs_articles.aid')
                    ->join('hs_relation','hs_relation.aid','=','hs_member.id')
                    ->join('hs_editor','hs_editor.id','=','hs_relation.eid')
                    ->join('hs_books','hs_books.id','=','hs_articles.bid')
                    ->where(['hs_editor.id'=>$request->session()->get('id'),'hs_articles.status'=>'1'])
                    ->select('hs_articles.id','hs_articles.title','hs_articles.isfree','hs_articles.status','hs_member.name','hs_books.b_name')
                    ->paginate(4);
        // dd($articles);
        return view('admin.editor.article.index',['articles'=>$articles]);
    }

    public function getAllow(Request $request)
    {
    	$id = $request->input('id');
    	$status = $request->input('sh');
    	$res = DB::table('hs_articles')->where('id','=',$id)->update(['status'=>$status]);
    	// return $id;
    	if($res){
    		return redirect('/admin/earticle/index')->with('success','操作成功');
    	}else{
    		return redirect('/admin/earticle/index')->with('error','操作失败');
    	}
    }

    public function getForbid($id)
    {
    	return $id;
    }

     public function getRead($id)
    {
    	$article = DB::table('hs_articles')->where('id',$id)->select('id','title','content')->first();
    	// dd($article);
    	return view('admin.editor.article.read',['article'=>$article]);
    }

}
